#include "city_search_handler.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QMouseEvent>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QKeyEvent>
#include <QTimer>
#include <QDebug>


city_search_handler::city_search_handler(QLineEdit *inputEdit, QLabel *resultLabel, QLabel *searchLabel, QObject *parent)
    : QObject(parent), m_inputEdit(inputEdit), 
      m_resultLabel(resultLabel), 
      m_searchLabel(searchLabel),
      m_cityId(""),
      m_cityName("")
{
    // 关键：去除QLineEdit边框并设置透明背景
       m_inputEdit->setStyleSheet(R"(
           QLineEdit {
               border: none;               /* 去除边框 */
             background:#F6FAFE;    /* 背景透明 */
               padding: 2px;               /* 适当内边距 */
               font-size: 14px;            /* 字体大小 */
               color: #333333;             /* 文字颜色 */
           }
           QLineEdit:focus {
               outline: none;              /* 去除聚焦时的虚线边框 */
           }
       )");
       
       // 配置输入框提示文字
       m_inputEdit->setPlaceholderText("请输入城市名称...");
       
       // 配置搜索标签
       m_searchLabel->setCursor(Qt::PointingHandCursor);
       m_searchLabel->setFont(QFont("Arial", 16)); // 适当放大搜索图标
       m_searchLabel->installEventFilter(this);
       
       // 初始化延迟搜索定时器
       m_searchTimer = new QTimer(this);
       m_searchTimer->setSingleShot(true);
       m_searchTimer->setInterval(300); // 300ms延迟避免频繁搜索
       
       // 关联信号槽
       connect(m_searchTimer, &QTimer::timeout, this, &city_search_handler::onSearchClicked);
       connect(m_inputEdit, &QLineEdit::textChanged, this, &city_search_handler::onTextChanged);
}


bool city_search_handler::eventFilter(QObject *obj, QEvent *event) {
    if (obj == m_searchLabel && event->type() == QEvent::MouseButtonRelease) {
        onSearchClicked();
        return true;
    }
    return QObject::eventFilter(obj, event);
}

void city_search_handler::onTextChanged(const QString &text) {
    if (text.isEmpty()) {
        m_resultLabel->clear();
        m_cityId.clear();
        m_cityName.clear();
        return;
    }
    m_searchTimer->start();
}

void city_search_handler::onSearchClicked() {
    QString cityName = m_inputEdit->text().trimmed();
    if (cityName.isEmpty()) return;

    if (searchCity(cityName)) {
        m_resultLabel->setText(m_cityName);
        qDebug()<<"找到城市"<<m_cityName;
        qDebug() << "城市ID:" << m_cityId;
    } else {
        m_resultLabel->setText("未找到城市");
    }
    // 发射搜索完成信号
        emit searchCompleted();
}

bool city_search_handler::searchCity(const QString &cityName) {
    QFile file(":/城市");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QMessageBox::critical(m_inputEdit->parentWidget(), "错误", "无法打开城市数据文件");
        return false;
    }

    QTextStream in(&file);
    in.setCodec("UTF-8");  // 明确设置文件编码为 UTF-8
    bool found = false;
    while (!in.atEnd()) {
        QString line = in.readLine();
        QStringList parts = line.split('\t');
        if (parts.size() == 2 && parts[1] == cityName) {
            m_cityId = parts[0];
            m_cityName = parts[1];
            found = true;
            break;
        }
    }
    file.close();
    return found;
}

